function D = qr2(A,epsilon,show)
% D = qr2(A,epsilon,show)
% To find the eigenvalues of a symmetric tridigonal matrix.
% The QR method is employed.
% A is an n x n symmetric matrix, input.
% epsilon is the tolerance, input.
% D is the vector of eigenvalues, output.
if nargin==2, show = 0; end
[n,n] = size(A);
k = 1;
m = n;
cnt = 0;
while m > 1
  S = A(m-1:m,m-1:m);
  if abs(S(2,1)) < epsilon*(abs(S(1,1)) + abs(S(2,2)))
    A(m,m-1) = 0;
    A(m-1,m) = 0;
    m = m-1;
  else
    shift = eig(S);
    if abs(shift(1)-A(m,m))<abs(shift(2)-A(m,m))
      shift = shift(1); 
    else
      shift = shift(2); 
    end
    [Q,R] = qr(A-shift*eye(n));
    A = R*Q + shift*eye(n);
    cnt = cnt+1;
    if show==1,
      home,  if cnt==1, clc; end;
      disp(''),disp(['Symmetric tridiagonal QR iteration No. ',...
      int2str(cnt)]),disp(''),disp(A),pause(0.75);
    end
  end
  k = k+1;
end
D = diag(A);
